/**
 * @file mmc_test.c
 * @author Rocky
 * @brief mmc测试程序，emmc挂载在/mnt目录下
 * @version 0.1
 * @date 2024-03-29
 * 
 * @copyright Copyright (c) 2024
 * 
 */

#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <stdio.h>
#include <stdlib.h>
#include "common.h"

#define PROGRAM_NAME "mmc_test"

int main(int argc, char *argv[])
{
    int fd = -1;
    int res = 0;

    char filename[] = "/mnt/test.txt";
    char write_dat[] = "Hello World!";
    char read_buf[128] = {0};

    /* 写入文件 */
    fd = open(filename, O_RDWR | O_CREAT, 0664);
    if (fd < 0)
        errmsg_die("%s file open fail,errno = %d.\r\n", filename, errno);

    res = write(fd, write_dat, sizeof(write_dat));
    if (res < 0)
    {
        errmsg_die("write dat fail,errno = %d.\r\n", errno);
    }
    else
    {
        printf("write %d bytes:%s\r\n", res, write_dat);
    }

    // 写入之后立马同步数据
    res = fsync(fd);
    if (res < 0)
        errmsg_die("fsync fail,errno = %d.\r\n", errno);

    close(fd);

    /* 读取文件 */
    fd = open(filename, O_RDONLY);
    if (fd < 0)
        errmsg_die("%s file open fail,errno = %d.\r\n", filename, errno);

    res = read(fd, read_buf, sizeof(read_buf));
    if (res < 0)
    {
        errmsg_die("read dat fail,errno = %d.\r\n", errno);
    }
    else
    {
        printf("read %d bytes:%s\r\n", res, read_buf);
    }

    close(fd);

    return 0;
}
